#include "timer.h"
#include "LED\led.h"

#define timeInt0 5000		//scanKey
#define timeInt1 600		//scanLEd

/***************************
	P3 for Scan key	   (Timer 0)
	Scan LED (Timer 1)	 
	**************************/

//public variable

extern unsigned char flag;
extern unsigned char flag_control;
extern unsigned char xyzVictim;			

//private variable
unsigned char value_display;
extern unsigned int  counter0;
unsigned int counter1;

void init_timer0()
{
	IE &= ~(0x02);// enable timer 0
	IE |= 0x02;
	
	TMOD &= ~(0x01);// setup mode for timer 0
	TMOD |= 0x01;	

	TH0 = (-10000/256); 
	TL0 = (-10000%256);

	TR0 = 1;//start timer0
	
}

void init_timer1()
{
	IE &= ~(0x08);// enable timer 1
	IE |= 0x08;
	
	TMOD &= ~(0x10);// setup mode for timer 1
	TMOD |= 0x10;	

	TH1 = (-10000/256); 
	TL1 = (-10000%256);

	TR1 = 1; //start timer1
	
}
 /*
void start_timer0()
{
	TR0 = 1;
}

void start_timer1()
{
	TR1 = 1;
}

void stop_timer0()
{
	TR0 = 0;
}

void stop_timer1()
{
	TR1 = 0;
}
*/
void ScanKey_isr() interrupt 1	//Timer 0
{
	TR1 = 0;//stop timer 1
	TR0 = 0;//stop timer 0
	// reinitialize
	TH0 = (-timeInt0/256);  
	TL0 = (-timeInt0%256);

	//For ScanKey
	scanKey();
	if(counter0>0){
		counter0--;
		flag=0;
		//P2^=0x80;
	}else{
		flag=1;
	}
	TR0 = 1;//start timer 0	
	TR1 = 1;//start timer 1	
}

void ScanLed_isr() interrupt 3 	 //Timer 1
{
	TR1 = 0;//stop timer 1
	TR0 = 0;//stop timer 0
	// reinitialize
	TH1 = (-timeInt1/256); 
	TL1 = (-timeInt1%256);
		  
	scan_led();
	counter1++;
	if(counter1>1000){
		counter1=0;
		//flag^=0x01;
		//P2^=0x80;
	}
	if(flag_control==2)
		if((counter1/2)>200){
			setbit((xyzVictim%64)/16,(xyzVictim%16)/4,xyzVictim%4);
		}else{
			clearbit((xyzVictim%64)/16,(xyzVictim%16)/4,xyzVictim%4);
		}
	TR0 = 1;//start timer 0	
	TR1 = 1;//start timer 1	
}


